package com.peas.platform.module.icims.base.sys.component.vo;

import lombok.extern.slf4j.Slf4j;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.text.PDFTextStripper;
import org.apache.pdfbox.text.TextPosition;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;

@Slf4j
public class PdfBoxKeyWordPosition extends PDFTextStripper {

    private int foundIndex = 0;

    //关键字字符数组
    private final char[] key;
    //坐标信息集合
    private final List<float[]> list = new ArrayList<>();
    //当前页信息集合
    private final List<float[]> pagelist = new ArrayList<>();
    private final List<TextPosition> foundPosition = new ArrayList<>();

    public PdfBoxKeyWordPosition(String keyWords) throws IOException {
        super();
        super.setSortByPosition(true);
        char[] key = new char[keyWords.length()];
        for (int i = 0; i < keyWords.length(); i++) {
            key[i] = keyWords.charAt(i);
        }
        this.key = key;
    }

    //获取坐标信息
    public List<float[]> getCoordinate(PDDocument doc) {
        try {
            assert doc != null;
            super.document = doc;
            int pages = doc.getNumberOfPages();
            for (int i = 1; i <= pages; i++) {
                pagelist.clear();
                super.setSortByPosition(true);
                super.setStartPage(i);
                super.setEndPage(i);
                Writer dummy = new OutputStreamWriter(new ByteArrayOutputStream());
                super.writeText(document, dummy);
                for (float[] li : pagelist) {
                    li[2] = i;
                }
                list.addAll(pagelist);
            }
            return list;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
        return list;
    }

    //获取坐标信息
    @Override
    protected void writeString(String string, List<TextPosition> textPositions) {
        for (int i = 0; i < textPositions.size(); i++) {
            String str = textPositions.get(i).getUnicode();
            if (str.equals(key[foundIndex] + "")) {
                foundIndex++;
                foundPosition.add(textPositions.get(i));
                int count = foundIndex;
                for (int j = foundIndex; j < key.length; j++) {
                    String s;
                    if (i + j >= textPositions.size())
                        break;
                    else
                        s = textPositions.get(i + j).getUnicode();
                    //System.out.println(s);
                    if (s.equals(key[j] + "")) {
                        count++;
                    }
                }
                if (count == key.length) {
                    foundIndex = 0;
                    float[] idx = new float[3];
                    //idx[0] = textPositions.get(i).getX() + key.length * textPositions.get(i).getWidth() / 2;
                    //idx[1] = textPositions.get(i).getY() - textPositions.get(i).getHeight();
                    //idx[2] = textPositions.get(i).getUnicode();
                    //关键字到Y轴的距离=>X坐标
                    //idx[0] = textPositions.get(i).getY() - 2 * textPositions.get(i).getHeight();
                    //idx[0] = textPositions.get(0).getY();
                    idx[0] = foundPosition.get(0).getY();
                    //关键字到X轴的距离=>Y坐标
                    //idx[1] = textPositions.get(0).getX();
                    idx[1] = foundPosition.get(0).getX();
                    pagelist.add(idx);
                }
            } else {
                foundPosition.clear();
            }
        }
    }

}
